package testTransfoUML2Java;


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.uml2.uml.Type;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import uml2Java.JavaChargeur;
import uml2Java.TransformPackagesAndTypes;
import uml2Java.UMLChargeur;
import astjava.JavaFactory;

public class TestTransformPackagesAndTypes {
	
	private static org.eclipse.uml2.uml.Package _umlPackage;
	
	private static astjava.Model _javaModel;
	
	private static TransformPackagesAndTypes _transformer;

	private static Map<String,String> _correspondance;

	@BeforeClass
	public static void OneTimeSetup() {
		_javaModel = JavaFactory.eINSTANCE.createModel();
		UMLChargeur chargeur = new UMLChargeur("modele_de_test/testPackagesAndTypes.uml");
		_umlPackage = chargeur.loadUMLPackage();
		
		_correspondance = new HashMap<String,String>();
		_correspondance.put("EBoolean", "boolean");
		_correspondance.put("EByte", "byte");
		_correspondance.put("EChar", "char");
		_correspondance.put("EDouble", "double");
		_correspondance.put("EFloat", "float");
		_correspondance.put("EInt", "int");
		_correspondance.put("ELong", "long");
		_correspondance.put("EShort", "short");
		_correspondance.put("EString", "String");
		
		_transformer = new TransformPackagesAndTypes(_umlPackage, _javaModel);
		_transformer.doTransformPackagesAndTypes();
	}
	
	@AfterClass
	public static void OneTimeTearDown() {
		JavaChargeur chargeur = new JavaChargeur("modele_de_test/testPackagesAndTypes.astjava");
		chargeur.save(_javaModel);
	}
	
	@Test
	public void testTransformPackage() {
		assertTrue(_javaModel.getOwnedElements().size()==1);
		assertEquals(_umlPackage.getName(), _javaModel.getOwnedElements().get(0).getName());
	}
	
	@Test
	public void testGenerationPrimitiveTypes() {		
		for (String nameJava : _correspondance.values()) {
			boolean trouve = false;
			for (astjava.Type javaPrimitiveType :_javaModel.getOrphanTypes()) 
				if (javaPrimitiveType.getName().equals(nameJava))
					trouve = true;
			assertTrue(trouve);
		}
	}
	
	@Test
	public void testPrimitiveTypesInSymbolMap() {
		assertNotNull(_transformer.get_symbolMap());
		for (String key : _correspondance.keySet()) {
			assertNotNull(key,_transformer.get_symbolMap().get(key));
		}
	}
	
	@Test
	public void testModelTypesInSymbolMap() {
		for (Type umlType : _umlPackage.getOwnedTypes()) {
			assertNotNull(umlType.getName(),_transformer.get_symbolMap().get(umlType.getName()));
		}
	}
	
	@Test
	public void testTransformTypes() {
		assertFalse(_javaModel.getOwnedElements().get(0).getOwnedElements().size()==0);
		assertEquals(_javaModel.getOwnedElements().get(0).getOwnedElements().size(), _umlPackage.getOwnedTypes().size());
	}

}
